package travellersalesman;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;


import stochastic.Solution;

public class GraphPanel implements GUI.IVizualizer {

	@Override
	public void draw(Solution s, JPanel p) {
		TravellerPath ctp = (TravellerPath)s;
		RealExtendetGraph gr = (RealExtendetGraph)ctp.getGraph();
		Graphics g = p.getGraphics();
		
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, p.getWidth(), p.getHeight());
		double xmin=Double.POSITIVE_INFINITY,xmax=Double.NEGATIVE_INFINITY;
		double ymin=Double.POSITIVE_INFINITY,ymax=Double.NEGATIVE_INFINITY;
		
		for(int i=0;i<gr.getDimension();++i) {
			double x = gr.getPoint(i).x;
			double y = gr.getPoint(i).y;
			if(x <xmin)
				xmin = x;
			else if(x>xmax)
				xmax = x;
			
			if(y<ymin)
				ymin = y;
			else if(y>ymax)
				ymax = y;
		}
		
		double dx = (xmax-xmin)/(p.getWidth()-10);
		double dy = (ymax-ymin)/(p.getHeight()-10);

		
		for(int i=0;i<gr.getDimension();++i) {
			double x = gr.getPoint(i).x-xmin;
			double y = gr.getPoint(i).y-ymin;
			
			int myx = (int)(x/dx)+6;
			int myy = p.getHeight()-(int)(y/dy)-6;
			
			g.setColor(Color.BLACK);
			g.drawOval(myx, myy, 3, 3);

			
			g.setColor(Color.RED);
			g.fillOval(myx, myy, 3, 3);
		}
		
		Integer path[] = ctp.getPath();
		for(int i=0;i<path.length;++i) {
			double x1 = gr.getPoint(path[i]).x-xmin;
			double y1 = gr.getPoint(path[i]).y-ymin;
			
			double x2 = gr.getPoint(path[(i+1)%path.length]).x-xmin;
			double y2 = gr.getPoint(path[(i+1)%path.length]).y-ymin;
			
			int myx1 = (int)(x1/dx)+6;
			int myx2 = (int)(x2/dx)+6;
			int myy1 = p.getHeight()-(int)(y1/dy)-6;
			int myy2 = p.getHeight()-(int)(y2/dy)-6;
			
			g.drawLine(myx1, myy1, myx2, myy2);
		}
		
	}
	
}
